﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using BLL.DataContextClasses;

namespace BLL.BusinessClasses
{
    public class Carrera
    {
        //Campos
        private int id;
        private string descripcion;
        private AreaAcademica areaAcademica;

        //Propiedades
        public int ID
        {
            get { return this.id; }
        }
        public string Descripcion
        {
            get { return this.descripcion; }
            set { this.descripcion = value; }
        }
        public AreaAcademica AreaAcademica
        {
        get {if (this.areaAcademica == null) this.areaAcademica = AreaAcademica.GetAreaAcademicaByCarreraID(this.ID);
                return this.areaAcademica;}
        
        set{this.areaAcademica = value;}
        }

        //Constructores
        public Carrera(int IDToSearch)
        {
            try
            {
                DataClassesDataContext DB = new DataClassesDataContext();

                DataContextClasses.Carrera Car = (from c in DB.Carreras
                                                  where c.ID == IDToSearch
                                                  select c).First();

                this.id = Car.ID;
                this.descripcion = Car.Descripcion;
            }
            catch (SqlException SqlEx)
            {
                throw new Exception("No existe tal carrera\n" + SqlEx.Message);
            }
        }

        //Metodos
        public static Carrera GetCarreraByVacanteID(int VacanteID)
        {
             try
             {
                 DataClassesDataContext DB = new DataClassesDataContext();
                 
                 DataContextClasses.Vacante Vac = (from v in DB.Vacantes
                                                   where v.ID == VacanteID
                                                   select v).First();
                 
                 Carrera Carrera = new Carrera(Vac.ID_Carrera);
                 return Carrera;
             }
             catch (SqlException SqlEx)
             {
                 throw new Exception("Hubo un problema con la base de datos\n" + SqlEx.Message);
             }
        }
        public static List<Carrera> GetCarrerasByAreaAcademicaID(int AreaAcademicaID) 
        {
            try 
            {
                DataClassesDataContext DB = new DataClassesDataContext();

                var IDCarreras = from c in DB.Carreras
                                 where c.ID_AreaAcademica == AreaAcademicaID
                                 select c.ID;

                List<Carrera> CarrerasDeAreaAcademica = new List<Carrera>();

                foreach(int CarreraID in IDCarreras)
                {
                    CarrerasDeAreaAcademica.Add(new Carrera(CarreraID));
                }

                return CarrerasDeAreaAcademica;
            }
            catch (SqlException SqlEx)
            {
                throw new Exception("Hubo un problema con la base de datos\n" + SqlEx.Message);
            }
        }

        public static List<Carrera> GetCarrerasByUsuarioEgresadoID(int UsuarioEgresadoID)
        {
            try
            {
                DataClassesDataContext DB = new DataClassesDataContext();

                var Carreras = (from c in DB.Egresados_Carreras
                                where c.ID_Egresado == UsuarioEgresadoID
                                select c);

                List<Carrera> CarrerasDeEgresado = new List<Carrera>();

                foreach (var item in Carreras)
                {
                    CarrerasDeEgresado.Add(new Carrera(item.ID_Carrera));
                }
                return CarrerasDeEgresado;
            }
            catch (SqlException SqlEx)
            {
                throw new Exception("Hubo un problema con la base de datos\n" + SqlEx.Message);
            }
        }
        public static Carrera GetCarreraByUsuarioEstudianteID(int UsuarioEstudianteID)
        {
            try
            {
                DataClassesDataContext DB = new DataClassesDataContext();

                int ID_Carrera = (from e in DB.Estudiantes
                                  where e.ID == UsuarioEstudianteID
                                  select e.ID_Carrera).First();

                return new Carrera(ID_Carrera);
            }
            catch (SqlException SqlEx)
            {
                throw new Exception("Hubo un problema con la base de datos\n" + SqlEx.Message);
            }
        }
    }
}
